package com.feng.exma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.feng.common.consts.ExamContant;
import com.feng.common.enums.ApiErrorEnum;
import com.feng.common.exceptions.ServiceException;
import com.feng.common.vo.ApiRest;
import com.feng.exma.dto.ExamSaveDto;
import cn.hutool.core.lang.Opt;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.common.dto.PageDto;
import com.feng.common.exceptions.ServiceException;
import com.feng.common.vo.PageResult;
import com.feng.exma.entity.Exam;
import com.feng.exma.entity.ExamDepart;
import com.feng.exma.entity.ExamUser;
import com.feng.exma.mapper.ExamDepartMapper;
import com.feng.exma.mapper.ExamMapper;
import com.feng.exma.mapper.ExamRepoMapper;
import com.feng.exma.mapper.ExamUserMapper;
import com.feng.exma.service.ExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author feng
 * @since 2023-03-22 17:35:29
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService {

    @Autowired
    private ExamRepoMapper examRepoMapper;

    @Autowired
    private ExamDepartMapper examDepartMapper;

    @Autowired
    private ExamUserMapper examUserMapper;

    @Transactional
    @Override
    public ApiRest create(ExamSaveDto examSaveDto) {
        checkData(examSaveDto);

        //save exam
        Exam exam=new Exam();
        BeanUtil.copyProperties(examSaveDto,exam,true);
        exam.setId(IdWorker.getIdStr());
        exam.setUpdateTime(new Date());
        exam.setCreateTime(new Date());
        exam.setJoinType(ExamContant.ZUJUAN_TYPE_REPO);
        exam.setState(ExamContant.OPEN);
        this.baseMapper.insert(exam);

        //save exam repo
        examSaveDto.getExamRepos().forEach(item->{
            item.setId(IdWorker.getIdStr());
            item.setExamId(exam.getId());
            examRepoMapper.insert(item);
        });

        //save exam depart
        if (examSaveDto.getOpenType()== ExamContant.OPENT_DEPART) {//部门公开
            examSaveDto.getDepartIds().forEach(item->{
                ExamDepart examDepart=new ExamDepart();
                examDepart.setExamId(exam.getId());
                examDepart.setDepartId(item);
                examDepart.setId(IdWorker.getIdStr());
                examDepartMapper.insert(examDepart);
            });
        }

        //save exam user
        if (examSaveDto.getOpenType()== ExamContant.OPENT_SPECIFY) {//指定人员公开
            examSaveDto.getUserIds().forEach(item->{
                ExamUser examUser=new ExamUser();
                examUser.setExamId(exam.getId());
                examUser.setId(IdWorker.getIdStr());
                examUser.setUserId(item);
                examUserMapper.insert(examUser);
            });
        }

        return ApiRest.success();
    }

    public void checkData( ExamSaveDto examSaveDto){
        if(examSaveDto==null){
            throw new ServiceException("参数不能为空");
        }
        if (StringUtils.isEmpty(examSaveDto.getTitle())){
            throw new ServiceException(ApiErrorEnum.EXAM_CONTENT_IS_NULL);
        }

        //其他
    }

    @Override
    public PageResult<Exam> pagingExam(PageDto<Exam> pageDto) {
        if (pageDto == null) {
            throw new ServiceException("请求参数异常");
        }
        Integer nowPage = Opt.ofBlankAble(pageDto.getNow()).orElse(1);
        Integer size = Opt.ofBlankAble(pageDto.getSize()).orElse(5);
        Page<Exam> page = new Page<>(nowPage, size);
        List<Exam> exams = baseMapper.pagingExam(page, pageDto.getWhere());
        return new PageResult<>(page.getTotal(), exams, page.getPages());
    }
}
